package com.stu.chart.security;

import com.stu.chart.entity.Staff;
import com.stu.chart.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.Collections;

/**
 * @author dsj
 */
@Component
public class CustomUserDetailsService implements UserDetailsService/*, AuthenticationProvider*/ {
    @Autowired
    private StaffService loginservice;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Staff pwd=loginservice.getUser(username);
        if(pwd == null){
            throw new BadCredentialsException("用户不存在");
        }
        //TODO 校验账号是否被禁用
        // 然后再走密码认证
        return new User(username,pwd.getPassword(), Collections.emptyList());
    }

//    @Override
//    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
//        return new UsernamePasswordAuthenticationToken("123","123",Collections.emptyList());
//    }
//
//    @Override
//    public boolean supports(Class<?> aClass) {
//        return aClass.equals(UsernamePasswordAuthenticationToken.class);
//    }
}
